﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoolERP_BLL;
using CoolERP_Entities;
using System.Collections;

namespace CoolERP_Web.User
{
    public partial class XSDDMX : BasePage
    {
        private static Hashtable htYSLX = new Hashtable();
        private static Hashtable htYS = new Hashtable();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                BindData();
        }

        private void BindData()
        {
            using (CS_YSLXBLL yBLL = new CS_YSLXBLL())
            {
                foreach (CS_YSLX item in yBLL.GetAll())
                    htYSLX[item.BH] = item.MC;
            }
            using (CS_YSBLL yBLL = new CS_YSBLL())
            {
                foreach (CS_YS item in yBLL.GetAll())
                    htYS[item.YSLXBH + "," + item.BH] = item.MC;
            }
            vXSDD entity = new vXSDD();
            Dictionary<string, string> dc = new Dictionary<string, string>();
            dc.Add("ID", this.GetUrlParam("id").ToString());
            using (XSDDBLL xBLL = new XSDDBLL())
            {
                int RecordCount = 0;
                entity = xBLL.GetXSDD(dc, "BH", null, 100, out RecordCount).FirstOrDefault();
                if (entity != null)
                {
                    liBH.Text = entity.BH;
                    liKHMC.Text = entity.KHJC;
                    liKHLXR.Text = entity.LXR;
                    liLXFS.Text = entity.LXRSJ;
                    liDepartment.Text = entity.BMMC;
                    liSaler.Text = entity.RYMC;
                    liCHFS.Text = entity.CHFS;
                    liDHSJ.Text = entity.DHSJ.Value.ToString("yyyy-MM-dd HH:mm");
                    liCHSJ.Text = entity.CHSJ.Value.ToString("yyyy-MM-dd HH:mm");
                    liZT.Text = entity.DDZT != null && Convert.ToInt32(entity.DDZT) == 1 ? "已完成" : "未完成";
                }
            }
            using (XSDDMXBBLL mBLL = new XSDDMXBBLL())
            {
                //明细表
                List<XSDDMXB> mxbs = mBLL.GetList(t => t.XSDDID == entity.ID);
                GridView1.DataSource = mxbs;
                GridView1.DataBind();
                liMoney.Text = mxbs.Sum(t => t.SL * t.DJ).Value.ToString("C");
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Literal liCPLX = e.Row.FindControl("liCPLX") as Literal;
                Literal liYS = e.Row.FindControl("liYS") as Literal;
                XSDDMXB entity = e.Row.DataItem as XSDDMXB;
                if (entity.CPID == null)
                {
                    e.Row.Attributes.Add("style", "color:red"); 
                    liYS.Text = string.Format("{0} {1}", htYSLX[entity.CPYS.Split(',')[0]], Convert.ToString(htYS[entity.CPYS]));
                }
                else
                {
                    using (ZL_CPBLL cBLL = new ZL_CPBLL())
                    {
                        int RecordCount;
                        Dictionary<string, string> dc = new Dictionary<string, string>();
                        dc.Add("ID", entity.CPID.ToString());
                        List<vCP> itemLists = cBLL.GetCP(dc, "BH", 1, 1, out RecordCount);
                        if (itemLists.Count > 0)
                        {
                            vCP cp = itemLists.FirstOrDefault();
                            if (cp != null)
                                liCPLX.Text = cp.CPLXMC;
                        }
                    }
                    liYS.Text = entity.CPYS.Replace(',', ' ');
                }
            }
        }
    }
}